package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.domain.CheckItem;
import com.itheima.result.PageResult;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        System.out.println(queryString);
        Page<CheckItem> itemPage = checkItemDao.selectByCondition(queryString);
        System.out.println(itemPage.getResult());
        PageResult pageResult=new PageResult(itemPage.getTotal(),itemPage.getResult());
        return pageResult;
    }

    @Override
    public void delete(Integer id) throws RuntimeException {
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count>0){
            throw new RuntimeException("当前检查项被引用，不能删除");
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemDao.findById(id);
        return checkItem;
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
